<?php
/**
 * Created by PhpStorm.
 * User: 图图
 * Date: 2021-6-8
 * Time: 16:29
 */

namespace app\web\controller;
use app\common\controller\Api;
use app\common\model\Factor;
use app\common\model\Fenorder;
use app\web\controller\Express;
use think\Db;

/**
 * 门票设置控制器
 * @package app\index\controller
 */
class AdmissionTicket extends Auth
{
    public $noNeedLogin = [];

    /**
     *获取门票列表
     * @return array
     */
    public function lists()
    {
        if ($this->request->isPost()) {
            $userInfo = $this->userInfo;
            $page = $this->request->post('page')? '' : 1;
            $limit = $this->request->post('page')? '' : 10;
            $data = Db::name('qt_admission_ticket')
                ->field('a.*,u.real_name')
                ->alias('a')
                ->leftJoin('qt_users u','u.id = a.user_id')
                ->where(['a.store_id' => $userInfo['store_id']])
                ->order('a.id desc')
                ->paginate($limit)->each(function ($item){
                    $typeIds = explode(',',$item['hand_cards']);
                    foreach ($typeIds as $key=>$value){
                        $typename[$key] = Db::name('qt_hand_card_type')->where('id',$value)->value('name');
                    }
                    $item['type_name'] = implode(',',$typename);
                    //处理商品
                    $productIds = explode(',',$item['product_ids']);
                    foreach ($productIds as $key=>$value){
                        $productName[$key] = Db::name('qt_goods')->where('id',$value)->value('name');
                    }
                    $item['product_name'] = implode(',',$productName);
                    return $item;
                });
            $this->result('查询成功', $data, 200);
        }
        $this->result('无效的请求', '', 204);
    }



    /**
     * 添加门票
     */
    public function add(){
        if ($this->request->isPost()) {
            $getData= $this->request->post();
            if(empty($getData['hand_cards']) || empty($getData['product_ids']) || empty($getData['room_id']) || empty($getData['cancel_time']) || empty($getData['sort'])){
                $this->result('缺少参数', '', 204);
            }
            $goodsIds = explode(',',$getData['product_ids']);
            $find = Db::name('qt_goods')->whereIn('id',$goodsIds)->where(['status'=>'0'])->find();
            if(!empty($find)){
                $this->result('【'.$find['name'].'】不可售，无法添加门票', '', 204);
            }
            $insert = [
                'name'=>$getData['name'],
                'hand_cards'=>$getData['hand_cards'],
                'product_ids'=>$getData['product_ids'],
                'room_id'=>$getData['room_id'],
                'cancel_time'=>$getData['cancel_time'],
                'sort'=>$getData['sort'],
                'user_id'=>$this->userInfo['id'],
                'store_id'=>$this->userInfo['store_id'],
                'create_time'=>time(),
            ];
            $insert = Db::name('qt_admission_ticket')->insert($insert);
            if($insert){
                $this->result('添加成功', '', 200);
            }
            $this->result('添加失败', '', 204);
        }
        $this->result('无效的请求', '', 204);
    }

    /**
     * 编辑门票
     */
    public function save(){
        if ($this->request->isPost()) {
            $getData= $this->request->post();
            if(empty($getData['hand_cards']) || empty($getData['product_ids']) || empty($getData['room_id']) || !isset($getData['cancel_time']) || empty($getData['sort'])){
                $this->result('缺少参数', '', 204);
            }
            $update= Db::name('qt_admission_ticket')->where('id',$getData['id'])->update($getData);
            if($update !== false){
                $this->result('修改成功', '', 200);
            }
            $this->result('修改失败', '', 204);
        }
        $this->result('无效的请求', '', 204);
    }


    /**
     * 编辑门票
     */
    public function edit(){
        if ($this->request->isPost()) {
            $getData= $this->request->post();
            if(empty($getData['id'])){
                $this->result('缺少参数', '', 204);
            }
            $data= Db::name('qt_admission_ticket')->where('id',$getData['id'])->find();
            $data['hand_cards'] =  explode(',',$data['hand_cards']);
            if(!empty($data['hand_cards'])){
                foreach ($data['hand_cards'] as $key=>$value){
                    $data['hand_cards'][$key] = intval($value);
                }
            }
            $data['product_ids'] =  explode(',',$data['product_ids']);
            $find = Db::name('qt_goods')->whereIn('id',$data['product_ids'])->where(['status'=>'0'])->find();
            if(!empty($find)){
                $this->result('【'.$find['name'].'】不可售，无法添加门票', '', 204);
            }
            if(!empty($data['product_ids'])){
                foreach ($data['product_ids'] as $key=>$value){
                    $data['product_ids'][$key] = intval($value);
                }
            }
            $this->result('保存成功', $data, 200);
        }
        $this->result('无效的请求', '', 204);
    }


    /**
     * 删除门票
     */
    public function delete(){
        if ($this->request->isPost()) {
            $getData= $this->request->post();
            if(empty($getData['id'])){
                $this->result('缺少参数', '', 204);
            }
            $data= Db::name('qt_admission_ticket')->where('id',$getData['id'])->delete();
            $this->result('删除成功', '', 200);
        }
        $this->result('无效的请求', '', 204);
    }


}
